package reduce.fz.com.reduce.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.alibaba.fastjson.TypeReference;

import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.TeamDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.event.JoinOrQuitTeamEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

public class FindTeamFragment extends BaseListFragment<FindTeamFragment.TeamViewHolder, Team> {
    private Context context;
    private LayoutInflater layoutInflater;
    //分类ID
    private Long categoryid = null;
    //分类下的小组
    private static final int TYPE_OF_CATEGORY = 0;
    //热门下的小组
    private static final int TYPE_OF_HOT = 1;

    private int type = 0;

    //创建分类下Fragment
    public static FindTeamFragment create(Long categoryid) {
        FindTeamFragment fragment = new FindTeamFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("categoryid", categoryid);
        bundle.putInt("type", TYPE_OF_CATEGORY);
        fragment.setArguments(bundle);
        return fragment;
    }


    //创建热门圈子
    public static  FindTeamFragment hotCreate() {
        FindTeamFragment fragment = new FindTeamFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", TYPE_OF_HOT);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        categoryid = getArguments().getLong("categoryid");
        type=getArguments().getInt("type");


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //重新渲染指定的圈子
    public void onEvent(JoinOrQuitTeamEvent event)
    {
        dataChange(event.team);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.context = view.getContext();
        this.layoutInflater = LayoutInflater.from(context);
    }

    public interface TeamCardListener {
        public void onClickCard(Team team);
    }

    @Override
    public TeamViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_team_card, parent, false);
        TeamViewHolder holder = new TeamViewHolder(view, new TeamCardListener() {
            @Override
            public void onClickCard(Team team) {
                Intent intent = new Intent(getActivity(), TeamDetailActivity.class);
                intent.putExtra("teamid", team.teamid);
                startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public TeamViewHolder createfootViewHolder(View view) {
        return new TeamViewHolder(view);
    }

    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<Team>>(new TypeReference<Result<List<Team>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<Team>> data) {
                addResult(data);
            }
        });
    }

    @Override
    protected String setUrlBeforeRequest(List<Team> datas, Pagination pagination) {
        String url = "";
        //翻页需要的startnum需要在这里面设置
        if (type == TYPE_OF_CATEGORY)
            url = ApiUtil.getApi(context, R.array.api_team_list, categoryid, pagination.getPageNo(), pagination.getPageSize());
        else
            url = ApiUtil.getApi(context, R.array.api_hot_team, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }


    @Override
    protected int getPageType() {
        //返回startnum分页类型
        return Pagination.TYPE_START_NUM;
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回普通列表
        return null;
    }

    public static class TeamViewHolder extends BaseRecycleViewHolder<Team> {
        private TextView mTeamTitle;
        private TextView mTeamJoinDesc;
        private NetworkImageView mTeamIcon;
        private TeamCardListener teamCardListener;
        private View itemView;
        //爱心
        private TextView mLoveScore;
        //社交
        private TextView mSocialScore;
        //活力
        private TextView mEnergyScore;
        //智力
        private TextView mIntellScore;

        private ImageView mIsJoined;


        public TeamViewHolder(View itemView, TeamCardListener teamCardListener) {
            this(itemView);
            this.teamCardListener = teamCardListener;
        }


        public TeamViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            //爱心值
            mLoveScore = (TextView) itemView.findViewById(R.id.tv_love_score);
            //社交值
            mSocialScore = (TextView) itemView.findViewById(R.id.tv_social_score);
            //活力值
            mEnergyScore = (TextView) itemView.findViewById(R.id.tv_energy_score);
            //智力值
            mIntellScore = (TextView) itemView.findViewById(R.id.tv_intell_score);
            //标题
            mTeamTitle = (TextView) itemView.findViewById(R.id.tv_title);
            mTeamJoinDesc = (TextView) itemView.findViewById(R.id.team_join_desc);
            mTeamIcon = (NetworkImageView) itemView.findViewById(R.id.iv_team_icon);
            mIsJoined = (ImageView) itemView.findViewById(R.id.iv_isJoined);
        }


        @Override
        public void renderView(final Team data, int position) {
            mTeamTitle.setText(data.title);
            mLoveScore.setText(data.love_score + "");
            mSocialScore.setText(data.social_score + "");
            mIntellScore.setText(data.intell_score + "");
            mEnergyScore.setText(data.energy_score + "");
            if (data.teamAttach.isJoin)
                mIsJoined.setVisibility(View.VISIBLE);
            else
                mIsJoined.setVisibility(View.GONE);
            mTeamIcon.setImageUrl(data.iconurl, HttpUtils.getImageLoader());
            mTeamJoinDesc.setText("共有" + data.teamAttach.joinnum + "参与中");
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    teamCardListener.onClickCard(data);
                }
            });


        }
    }


}
